<?php

namespace App\Http\Controllers\Admin;

use App;
use App\Http\Controllers\Controller as BaseController;
use App\Http\Logic\Admin\AdminLogic;
use App\Common\Status;
use Exception;
use App\Http\Requests\AdminRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AdminController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $params   = $request->query();
        $pageSize = $request->query("pageSize", Status::PAGE_SIZE);
        $page     = $request->query("page", Status::PAGE);
        $data     = AdminLogic::getAdminList($params,$pageSize, $page);
        return $this->success(Status::SUCCESS, 'success', $data);
    }


    /**
     * @param AdminRequest $request
     * @return JsonResponse
     * @throws Exception
     */
    public function createAdmin(AdminRequest $request): JsonResponse
    {
        $params = $request->validated();
        $id     = AdminLogic::createAdmin($params);
        $r      = $params['id'] ? [] : ['id' => $id];
        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function dtlAdmin($id): JsonResponse
    {
        $info = AdminLogic::getAdminDtl($id);
        return $this->success(Status::SUCCESS, 'success', $info);
    }


    /**
     * @param $id
     * @return JsonResponse
     */
    public function delAdmin($id): JsonResponse
    {
        if($id == 1) {
            return $this->error(Status::ERROR, '无权删除');
        }
        AdminLogic::delAdmin($id);
        return $this->success(Status::SUCCESS, 'success', []);
    }

}
